package com.mz.ozy.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.bean.GoodsListBean;
import com.mz.ozy.utils.CalendarUtils;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by administrator on 2018/11/21.
 */
public class SearchListAdapter extends BaseQuickAdapter<GoodsListBean.GoodsPageBean.PageItemsBean, BaseViewHolder> {

    private long curTime;

    public SearchListAdapter(@Nullable List<GoodsListBean.GoodsPageBean.PageItemsBean> data) {
        super(R.layout.item_search_list, data);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        Date curDate = cal.getTime();
        curTime = curDate.getTime();
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsListBean.GoodsPageBean.PageItemsBean item) {
        helper.setText(R.id.tv_goods_name, item.getTradename());
        if (!TextUtils.isEmpty(item.getStarttime())) {
            Date date = null;
            try {
                date = CalendarUtils.string2Date(item.getStarttime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (curTime >= date.getTime()) {
                helper.setText(R.id.tv_benifit_kind, item.getDescribed());
            } else {
                helper.setText(R.id.tv_benifit_kind, "预计发货时间:" + CalendarUtils.date2String(date));
            }
        } else {
            helper.setText(R.id.tv_benifit_kind, item.getDescribed());
        }
        if (TextUtils.isEmpty(item.getWeight())) {
            helper.setText(R.id.tv_unit_price, "");
        } else {
            helper.setText(R.id.tv_unit_price, item.getWeight() + "g");
        }
        if (TextUtils.isEmpty(item.getShopbv()) || Double.parseDouble(item.getShopbv()) == 0.0) {
            helper.setGone(R.id.tv_shop_bv, false);
        } else {
            helper.setVisible(R.id.tv_shop_bv, true);
            helper.setText(R.id.tv_shop_bv, item.getShopbv() + "BV");
        }

        String price = "";
        if (!TextUtils.isEmpty(item.getPrice()) && Double.parseDouble(item.getPrice()) != 0.00) {
            price += "¥";
            price += item.getPrice();
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += item.getMarket_price();
                price += "积分";
            } else {
                price += item.getMarket_price();
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }

        helper.setText(R.id.tv_goods_price, price);
        ImageView ivGoodsPic = helper.getView(R.id.iv_goods_pic);
        String imgUrl = "";
        if (!TextUtils.isEmpty(item.getImg_zt())) {
            final String[] img_arr = item.getImg_zt().trim().split(",");
            if (img_arr.length > 0) {
                imgUrl = img_arr[0];
            }
        }
        GlideUtils.loadRoundImg(mContext, ivGoodsPic, imgUrl, R.mipmap.default_foods_icon, 4);
        helper.addOnClickListener(R.id.ll_item_view);
        helper.addOnClickListener(R.id.iv_search_car);
        if ((helper.getAdapterPosition() - getHeaderLayoutCount()) / 2 == 0) {
            helper.setVisible(R.id.head_view_divider, true);
        } else {
            helper.setGone(R.id.head_view_divider, false);
        }
        if ((helper.getAdapterPosition() - getHeaderLayoutCount()) >= getData().size() - 2) {
            helper.setVisible(R.id.end_view_divider, true);
        } else {
            helper.setGone(R.id.end_view_divider, false);
        }
    }
}
